package command;

import java.util.ArrayList;
import java.util.HashMap;

import Exceptions.ColumnNotFoundException;
import Exceptions.SubselectReturnsMoreThan1RowException;

public class Condition extends ConditionNode{
	
	private String field;
	private Operator operator;
	private SelectStatement rights;
	private String right;
	
	public Condition(Operator c,String l, String r) {
		this.operator = c;
		this.field = l;
		this.right = r;
	}
	
	public Condition(Operator c,String l, SelectStatement r) throws SubselectReturnsMoreThan1RowException {
		this.operator = c;
		this.field = l;
		this.rights = r;
	}

	public ArrayList<ITreeNode> getChildren() {
		ArrayList<ITreeNode> a = new ArrayList<ITreeNode>();
		return a;
	}
	
	public String getField() {
		return field;
	}
	
	public Boolean eval(HashMap<String,String> hash) {
		if(rights == null) return operator.eval(hash.get(field),right);
		else {
			ResultNode a = rights.eval();
			return operator.eval(hash.get(field),field,a.getHeader(), a.getTable());
		}
	}
	
	public String getInfo() {
		return " " + field + " " + right;
	}
	
	public String toString() {
		if (right != null)
			return field +" "+ this.operator +" "+ right;
		if (rights != null)
			return  field +" "+  this.operator +" "+ rights;
		return "";
	}
}
